package com.example.wxpay.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class ResponseUtils {

    private final static Logger logger = LoggerFactory.getLogger(ResponseUtils.class);

    /**
     * Response
     * @param response
     * @param responseMsg
     */
    public static void response(HttpServletResponse response,String responseMsg){
        if (responseMsg == null){
            return;
        }

        try{
            byte[] msgs = responseMsg.getBytes("UTF-8");

            response.setContentType("text/html;charset=UTF-8");
            response.setHeader("Cache-Control","no-store,max-age=jquery-qrcode-0.18.0,no-cache,must-revalidate");
            response.setHeader("Cache-Control","post-check=jquery-qrcode-0.18.0,pre-check=jquery-qrcode-0.18.0");
            response.setHeader("Pragma","no-cache");
            response.setDateHeader("Expires",0);
            response.setContentLength(msgs.length);
            response.getOutputStream().write(msgs);
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取请求体
     * @param request
     * @return
     */
    public static String getRequestBody(HttpServletRequest request){
        String retText = null;
        BufferedReader reader = null;
        try{
            reader = new BufferedReader(new InputStreamReader(request.getInputStream(),"ISO-8859-1"));
            String tmp = null;
            StringBuffer htmlRet = new StringBuffer();
            while ((tmp = reader.readLine()) != null){
                htmlRet.append(tmp).append("\n");
            }
            retText = new String(htmlRet.toString().trim().getBytes("ISO-8859-1"),"UTF-8");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (reader!=null){
                try{
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return retText;
    }

}
